library(redist)
library(tidyverse)
library(maptools)
library(doMC)
source("../random_submap.R")

## Load map
fl <- readShapePoly("../../data/fl/FL.shp")

dir.create("../../data/largemap_enum_all", showWarnings = FALSE)
dir.create("/n/imai_lab/bfifield/enumeration/largemap_enum_all", showWarnings = FALSE)

## Get info on job
params <- expand.grid(nsims = 1:20,
                      ndists = c(75, 100))
i <- as.numeric(Sys.getenv("SLURM_ARRAY_TASK_ID"))

## -------------------
## Run enumeration alg
## -------------------

## ## Sample map
## fl_sub <- random_submap(fl, params$ndists[i])

## ## Convert shp file to adjacency list
## adjlist <- poly2nb(fl_sub, queen = FALSE)

## ## Sink
## adjlist_map <- c()
## for(k in 1:length(adjlist)){
##     sub <- adjlist[[k]]
##     sub <- sub[sub > k]
##     if(length(sub) > 0){
##         for(l in 1:length(sub)){
##             adjlist_map <- rbind(adjlist_map, c(k, sub[l]))
##         }
##     }
## }
## write_delim(
##     data.frame(adjlist_map),
##     path = paste0("../../data/largemap_enum_all/adjlist_map_", i, ".dat"),
##     col_names = FALSE
## )

## ## Order edges
## system(paste0("python ../ndscut.py <../../data/largemap_enum_all/adjlist_map_", i, ".dat >../../data/largemap_enum_all/adjlist_map_ordered_", i, ".dat"))

## ## Sink map nodes to be able to reconstruct submap
## sink(paste0("../../data/largemap_enum_all/map_sub_nodes_", i, ".dat"))
## cat(paste(rownames(fl_sub@data), collapse = " "), "\n")
## sink()

## Run enumpart
system(paste0("../../enumpart_private/enumpart ../../data/largemap_enum_all/adjlist_map_ordered_", i, ".dat -k 2 -allsols > /n/imai_lab/bfifield/enumeration/largemap_enum_all/solutions_", i, ".dat"))

